var postMessageDomain = "http://10.0.0.32:8080";
var domainHyperLinks = "http://0.0.0.0/?ID=";
var segmentHighligted;

var SUCCESS = 'success';
var INFO = "info";
var WARNING = "warning";
var DANGER = "danger";


window.addEventListener('message', function(event){
	if(event.origin !== postMessageDomain) return;
	console  .log('received response: ', event.data);
}, false);


function setThePDFFile(pdf){
	var iframe = document.getElementById('preview').contentWindow;
	console  .log("sending message");
	iframe   .postMessage(pdf, postMessageDomain);
}


function setSegmentOnSegmentList(segment){
	var id = new String(segment);
	window.parent.document.getSegmentFromPreview([id]);
}


function highLightSegment(segmentId){
	var segment = domainHyperLinks + segmentId;
		
	$('#preview').each(function() {
		$(".annotationLayer > a[class='selectedSegment']", this.contentWindow.document||this.contentDocument).removeClass("selectedSegment");
		$(".annotationLayer > a[href='" + segment + "']", this.contentWindow.document||this.contentDocument).addClass("selectedSegment");
	});	
}


function setPreviewAsDirty(message){

	$('#preview').each(function() {
		$( "#dirtyPreview", this.contentWindow.document||this.contentDocument ).remove();
		var div = jQuery('<div/>', {
	    	id: 'dirtyPreview',
	    	"class": 'dirtyPreview', 
	    	text: message
		});
	 	$("#toolbarContainer", this.contentWindow.document||this.contentDocument).append(div);
	});

}

function setPreviewAsClean(){

	$('#preview').each(function() {
		$( "#dirtyPreview", this.contentWindow.document||this.contentDocument ).remove();
	});
}


function addWelcomeMessage(message){
	removeWelcomeMessage();
	$('#preview').each(function() {


		var div = jQuery('<div/>', {
	    	"class": 'welcomeMessageClass', 
	    	text: message
		});

		$("#viewer", this.contentWindow.document||this.contentDocument).append(div);
	});
}

function removeWelcomeMessage(){
	$('#preview').each(function() {
		$( ".welcomeMessageClass", this.contentWindow.document||this.contentDocument ).remove();
	});
}


function showMessageInNotificationBar(message, type){

	var div = jQuery('<div/>', {
		id: 'notificationBar',
	    "class": 'notificationMessage', 
	    text: message
	});

	switch (type)
		{
		   case "success":
		   		div.addClass(SUCCESS);
		   break;
		   case "info":
				div.addClass(INFO);
		   break;
		   case "warning": 
				div.addClass(WARNING);
		   break;
		   case "danger": 
				div.addClass(DANGER);
		   break;

		   default: 
		   break;
		}

		$('#preview').each(function() {
			$( "#notificationBar", this.contentWindow.document||this.contentDocument ).remove();
			$("#toolbarContainer", this.contentWindow.document||this.contentDocument).append(div);
		});
}



function removeMessageFromNotificationBar(){

	$('#preview').each(function() {
		$( "#notificationBar", this.contentWindow.document||this.contentDocument ).remove();
	});
}


function getHighlightedSegment(){
	return segmentHighligted;
}



 $(window).load(function() {


 		addWelcomeMessage("This is a welcome message");

	   var iframeDoc = $('#preview').contents().get(0);

	   $(iframeDoc).on("click",'.annotationLayer a' , function (e) {
			clickHandler($(this));
	        e.preventDefault(); 
	    });
		
		function clickHandler(segmentInfo, e) {
			var segmentClicked = new String(segmentInfo[0]);
	    	var segment = segmentClicked.substring(segmentClicked.indexOf("ID=") + 3);
	    	alert("You've clicked on the segment: " + segment);
	    	segmentHighligted = segment;
	    	highLightSegment(segment);
	        /*setSegmentOnSegmentList(segment);*/
	    }


	    $(iframeDoc).on("click",'#zoomIn' , function (e) {
			setTimeout(function (){
				highLightSegment(segmentHighligted);
         	}, 500);  
	    });

	    $(iframeDoc).on("click",'#zoomOut' , function (e) {
			setTimeout(function (){
				highLightSegment(segmentHighligted);
         }, 500);  
	    });

		$(iframeDoc).on("click",'#scaleSelect' , function (e) {
			setTimeout(function (){
				highLightSegment(segmentHighligted);
         }, 500);  
	    });

	    $(iframeDoc).on("click",'#pageRotateCw' , function (e) {
			setTimeout(function (){
				highLightSegment(segmentHighligted);
         	}, 500);  
	    });

	    $(iframeDoc).on("click",'#pageRotateCcw' , function (e) {
			setTimeout(function (){
				highLightSegment(segmentHighligted);
         	}, 500);  
	    });


	    $(iframeDoc).on("click",'#previous' , function (e) {
			alert("Previous button clicked");
	    });


		$(iframeDoc).on("click",'#next' , function (e) {
			alert("Next button clicked");
	    });





	    /*Sticky Notes*/
	    /*$(iframeDoc).on("contextmenu", ".annotationLayer a", function( e ) {
	        e.preventDefault(); 
			var okIcon = "<span class=\"glyphicon glyphicon-ok\"></span>";
	        var cancelIcon = "<span class=\"glyphicon glyphicon-remove\"></span>";
	        var buttonOkIcon = "<button type=\"button\" class=\"btn btn-default btn-xs\">" + okIcon + "</button>";
	        var buttonCancelIcon = "<button type=\"button\" class=\"btn btn-default btn-xs\">" + cancelIcon + "</button>";
	        var stickButtons = "<div class=\"stickButtons\">" + buttonOkIcon + buttonCancelIcon + "</div>";
	        var inputContent = "<input type=\"text\" class=\"form-control\" placeholder=\"\">";
	        var stickContent = "<div class=\"stickContent\">" + inputContent + "</div>";
	        var stickTitle = "<div class=\"stickTitle\">" +"<h6>Pedro Viola.</h6>" + "</div>";
	        var stickContainer = "<div id=\"stickContainer\">" + stickTitle + stickContent + stickButtons + "</div>" 
			$('#preview').each(function() {
				$(".textLayer", this.contentWindow.document||this.contentDocument).append(stickContainer).offset({left:e.pageX,top:e.pageY});
			});
		});*/
	    
	});